package com.zxl.live.screen.screen.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.zxl.live.screen.R;

/**
 * Created by Administrator on 2018/1/23.
 */

public class ZzHorizontalProgressBar extends View {
    private int a;
    private int b;
    private int c;
    private int d;
    private int e;
    private Paint f;
    private Paint g;

    public ZzHorizontalProgressBar(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        TypedArray localTypedArray = context.obtainStyledAttributes(attributeSet, R.styleable.horizontal_progress);
        this.a = localTypedArray.getInteger(R.styleable.horizontal_progress_zpb_max, 100);
        this.b = localTypedArray.getInteger(R.styleable.horizontal_progress_zpb_progress, 0);
        this.c = localTypedArray.getColor(R.styleable.horizontal_progress_zpb_bg_color, getResources().getColor(android.R.color.white));
        this.d = localTypedArray.getColor(R.styleable.horizontal_progress_zpb_pb_color, getResources().getColor(android.R.color.holo_green_dark));
        this.e = localTypedArray.getDimensionPixelSize(R.styleable.horizontal_progress_zpb_padding, 2);
        localTypedArray.recycle();

        this.f = new Paint();
        this.f.setColor(this.d);
        this.f.setStyle(Paint.Style.FILL);
        this.f.setAntiAlias(true);
        this.g = new Paint();
        this.g.setColor(this.c);
        this.g.setStyle(Paint.Style.FILL);
        this.g.setAntiAlias(true);
    }

    private void a(Canvas paramCanvas) {
        int i = getWidth();
        if (i % 2 != 0) {
            i--;
        }
        int j = this.a;
        float f1 = 0.0F;
        if (j != 0) {
            f1 = 1.0F * this.b / this.a;
        }
        int k = getHeight() - 2 * this.e;
        if (k % 2 != 0) {
            k--;
        }
        float f2 = f1 * (i - 2 * this.e - k);
        paramCanvas.drawCircle(this.e + k / 2, this.e + k / 2, k / 2, this.f);
        paramCanvas.drawCircle(f2 + (this.e + k / 2), this.e + k / 2, k / 2, this.f);
        paramCanvas.drawRect(new RectF(this.e + k / 2, this.e, f2 + (this.e + k / 2), k + this.e), this.f);
    }

    private void b(Canvas paramCanvas) {
        int i = getHeight();
        if (i % 2 != 0) {
            i--;
        }
        int j = getWidth();
        if (j % 2 != 0) {
            j--;
        }
        paramCanvas.drawCircle(i / 2, i / 2, i / 2, this.g);
        paramCanvas.drawCircle(j - i / 2, i / 2, i / 2, this.g);
        paramCanvas.drawRect(new RectF(i / 2, 0.0F, j - i / 2, i), this.g);
    }

    public int getBgColor() {
        return this.c;
    }

    public int getMax() {
        return this.a;
    }

    public int getPadding() {
        return this.e;
    }

    public int getPercentage() {
        if (this.a == 0) {
            return 0;
        }
        return (int) (100.0D * this.b / this.a);
    }

    public int getProgress() {
        return this.b;
    }

    public int getProgressColor() {
        return this.d;
    }

    protected void onDraw(Canvas paramCanvas) {
        super.onDraw(paramCanvas);
        b(paramCanvas);
        a(paramCanvas);
    }

    protected void onMeasure(int paramInt1, int paramInt2) {
        super.onMeasure(paramInt1, paramInt2);
    }

    public void setBgColor(int paramInt) {
        this.c = paramInt;
        invalidate();
    }

    public void setMax(int paramInt) {
        this.a = paramInt;
        invalidate();
    }

    public void setPadding(int paramInt) {
        this.e = paramInt;
        invalidate();
    }

    public void setProgress(int paramInt) {
        this.b = paramInt;
        invalidate();
    }

    public void setProgressColor(int paramInt) {
        this.d = paramInt;
        invalidate();
    }
}
